package com.api.shopapi.controller;

import com.alibaba.fastjson.JSONObject;
import com.api.shopapi.entity.*;
import com.api.shopapi.entity.dto.AlarmRecordDTO;
import com.api.shopapi.service.*;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.transaction.interceptor.TransactionAspectSupport;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import java.util.*;

/**
 * 报警记录
 * */

@RestController
@RequestMapping("AlarmRecord")
public class AlarmRecordController  extends BaseController {

    @Autowired
    @Qualifier("AlarmRecordServiceImplOne")
    private AlarmRecordService alarmRecordService;

    @Autowired
    @Qualifier("StaffServiceImplOne")
    private StaffService staffService;

    @Autowired
    @Qualifier("UserServiceImplOne")
    private UserService userService;

    @RequestMapping(value = "Add", method = RequestMethod.POST)
    @Transactional
    public MyResult Add(@RequestBody JSONObject jsonObject) {

        try {
            String timeStr = jsonObject.getString("timeStr");
            String sign = jsonObject.getString("sign");
            String staffCode = jsonObject.getString("staffCode");

            String remarks = jsonObject.getString("remarks");
            String userCode = jsonObject.getString("userCode");
            Integer type = jsonObject.getInteger("type");
            String deviceName = jsonObject.getString("deviceName");
            String location = jsonObject.getString("location");
            Date createTime = jsonObject.getDate("createTime");
            PreCheck preCheck = checkSignAdmInterface(timeStr, sign,staffCode,"/AlarmRecord/Add");
            if (preCheck.getError() == 1) {
                return failed(preCheck.getMessage());
            }
            Staff byStaffCode = staffService.getByStaffCode(staffCode);
            AlarmRecord alarmRecord =new AlarmRecord();
            alarmRecord.setAlarmRecordCode(UUID.randomUUID().toString());
            alarmRecord.setCreateTime(createTime);
            alarmRecord.setRemarks(remarks);
            alarmRecord.setType(type);
            alarmRecord.setDeviceName(deviceName);
            alarmRecord.setLocation(location);
            alarmRecord.setUserCode(userCode);
            alarmRecord.setGovernmentCode(byStaffCode.getGovernmentCode());
            alarmRecordService.insert(alarmRecord);
            JSONObject jsonObject1 = new JSONObject();
            return success(jsonObject1);
        } catch (Exception e) {
            insertErrorForAffair("/AlarmRecord/Add!",jsonObject,e);
            TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
            return anomalous(getExceptionMsg(e));
        }

    }

    //报警记录
    @RequestMapping(value = "Page",method = RequestMethod.POST)
    public MyResult Page(@RequestBody JSONObject jsonObject){
        try {
            String timeStr = jsonObject.getString("timeStr");
            String sign = jsonObject.getString("sign");
            String staffCode = jsonObject.getString("staffCode");
            int pageIndex = jsonObject.getInteger("pageIndex");
            int pageSize = jsonObject.getInteger("pageSize");

            Date startTime = jsonObject.getDate("startTime");
            Date endTime = jsonObject.getDate("endTime");
            Integer type = jsonObject.getInteger("type");

            PreCheck preCheck = checkSignAdmInterface(timeStr, sign,staffCode,"/AlarmRecord/Page");

            if (preCheck.getError() == 1) {
                return failed(preCheck.getMessage());
            }
            Staff staff= staffService.getByStaffCode(staffCode);
            PageInfo<AlarmRecordDTO> alarmRecordPageInfo=alarmRecordService.getListPage(pageIndex,pageSize,type,startTime,endTime,staff.getGovernmentCode());
            List<AlarmRecordDTO> list = alarmRecordPageInfo.getList();
            for (AlarmRecordDTO alarmRecordDTO : list) {
                User byUserCode = userService.getByUserCode(alarmRecordDTO.getUserCode());
                alarmRecordDTO.setUser(byUserCode);
            }
            return success(alarmRecordPageInfo);
        } catch (Exception e) {
            insertError("/AlarmRecord/Page",jsonObject.toJSONString(),getExceptionMsg(e),"api");
            return anomalous(getExceptionMsg(e));
        }
    }

    //app报警列表byUserCode
    @RequestMapping(value = "PageByUserCode",method = RequestMethod.POST)
    public MyResult PageByUserCode(@RequestBody JSONObject jsonObject){
        try {
            String timeStr = jsonObject.getString("timeStr");
            String sign = jsonObject.getString("sign");
            String userCode = jsonObject.getString("userCode");
            int pageIndex = jsonObject.getInteger("pageIndex");
            int pageSize = jsonObject.getInteger("pageSize");

//            if(!checkSignPC(timeStr,sign)){
//                return failed("签名错误");
//            }

            PreCheck preCheck = checkSignFrontInterface(timeStr, sign,"/AlarmRecord/PageByUserCode");
            if (preCheck.getError() == 1) {
                return failed(preCheck.getMessage());
            }

            PageInfo<AlarmRecordDTO> alarmRecordPageInfo=alarmRecordService.getListPageByUserCode(pageIndex,pageSize,userCode);
            return success(alarmRecordPageInfo);
        } catch (Exception e) {
            insertError("/AlarmRecord/PageByUserCode",jsonObject.toJSONString(),getExceptionMsg(e),"api");
            return anomalous(getExceptionMsg(e));
        }
    }

}
